<template>
  <div>
    <el-dialog
      title="项目列表→报警短信"
      :visible.sync="show"
      :before-close="close"
      width="50%"
    >
      <div class="btns">
        <el-button type="primary" @click="addMessage" size="small"
          >新增</el-button
        >
        <el-button
          type="danger"
          @click="delMsg"
          size="small"
          :disabled="!delShow"
          >删除选中</el-button
        >
      </div>
      <div class="headerTable">报警短信</div>
      <el-table
        v-loading="loading"
        :data="messageList"
        border
        stripe
        :key="2"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="序号"
          align="center"
          type="index"
        ></el-table-column>
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column label="手机号" align="center" prop="phone" />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="messageEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              @click="messageDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="messageTotal > 0"
        :total="messageTotal"
        :page.sync="msgParams.pageNum"
        :limit.sync="msgParams.pageSize"
        @pagination="getMessageList"
        class="pagination-container"
      />
      <el-dialog
        :title="title"
        :visible.sync="innerVisible"
        width="30%"
        append-to-body
      >
        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialog" size="small">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')" size="small"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>
<script>
import {
  alarmMsgQuery, // 报警短信查询
} from "@/api/manage/asset";
import {
  alarmMsgAdd, // 报警短信新增
  alarmMsgDel, // 报警短信删除(单个或批量)
  alarmMsgUpdate, // 报警短信编辑
} from "@/api/manage/approval";
export default {
  name: "message",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: {
      default: "",
    },
  },
  data() {
    return {
      loading: false,
      msgParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: "",
        //    projectId
      },
      title: "新增",
      innerVisible: false,
      form: {
        name: "",
        phone: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
      },
      messageList: [],
      messageTotal: 0,
      magDialogShow: false,
      delItemList: [],
      delShow: false,
      msgId: "",
    };
  },
  created() {
    this.msgParams.projectId = this.id;
    this.getMessageList();
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val, "00000");
      this.delItemList = val.map((item) => {
        return {
          id: item.id,
        };
      });

      if (val.length > 0) {
        this.delShow = true;
      } else {
        this.delShow = false;
      }
    },
    getMessageList() {
      console.log("获取短信列表");
      alarmMsgQuery(this.msgParams).then((res) => {
        console.log(res, "-------");
        if (res.errorCode === "000000") {
          this.messageList = res.data.list;
          this.messageTotal = res.data.total;
        }
      });
    },
    close() {
      this.$emit("closeMsg");
    },
    messageEdit(row) {
      console.log(row);
      this.msgId = row.id;
      this.form.name = row.name;
      this.form.phone = row.phone;
      this.innerVisible = true;
      this.title = "编辑";
    },
    addMessage() {
      // 清空表单数据
      this.form = {
        name: "",
        phone: "",
      };
      // 重置表单验证状态
      this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.clearValidate();
        }
      });
      this.innerVisible = true;
      this.title = "新增";
      this.msgId = "";
    },
    delMsg() {
      let ids = [];
      console.log(this.delItemList);

      this.delItemList.forEach((item) => {
        ids.push(item.id);
      });
      console.log(ids, "11111111");

      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return alarmMsgDel(ids);
        })
        .then(() => {
          this.getMessageList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    messageDel(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return alarmMsgDel([row.id]);
        })
        .then(() => {
          this.getMessageList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    closeDialog() {
      this.innerVisible = false;
      // 清空表单数据
      this.form = {
        name: "",
        phone: "",
      };
      // 重置表单验证状态
      this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.clearValidate();
        }
      });
      this.msgId = "";
    },
    submitForm(formName) {
      console.log(this.$refs[formName], "-------------");

      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.msgId) {
            // 编辑
            const data = {
              ...this.form,
              id: this.msgId,
            };
            alarmMsgUpdate(data).then((res) => {
              if (res.errorCode === "000000") {
                this.closeDialog();
                this.$modal.msgSuccess("编辑成功！");
                this.form = {
                  name: "",
                  phone: "",
                };
                this.getMessageList();
              }
            });
          } else {
            const data = {
              ...this.form,
              projectId: this.id,
            };
            alarmMsgAdd(data).then((res) => {
              if (res.errorCode === "000000") {
                this.closeDialog();
                this.$modal.msgSuccess("新增成功！");
                this.form = {
                  name: "",
                  phone: "",
                };
                this.getMessageList();
              }
              console.log(res, "reeeeee");
            });
          }
        } else {
          return false;
        }
      });
      console.log("提交");
    },
  },
};
</script>
<style lang="scss" scoped>
.headerTable {
  background: #5d6d7f;
  height: 48px;
  line-height: 48px;
  text-align: center;
  width: 100%;
  color: #fff;
  font-weight: Bold;
  font-size: 14px;
  font-family:
    Helvetica Neue,
    Helvetica,
    PingFang SC,
    Hiragino Sans GB,
    Microsoft YaHei,
    Arial,
    sans-serif;
  border: 1px solid #e3e3e3;
  border-bottom: 0;
}
.btns {
  text-align: right;
  margin-bottom: 10px;
}
::v-deep .el-dialog__header {
  border-bottom: 1px solid #e9e9ec;
}
</style>
